package com.ruoyi.oa.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.oa.domain.vo.OaFinanceImprestVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.OaFinancePayment;
import com.ruoyi.oa.domain.vo.OaFinancePaymentVo;
import com.ruoyi.oa.domain.bo.OaFinancePaymentBo;
import com.ruoyi.oa.service.IOaFinancePaymentService;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.oa.domain.vo.OaProjectVo;


/**
 * 付款流程
 *
 * @author ruoyi
 * @date 2023-05-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/financePayment")
public class OaFinancePaymentController extends BaseController {

    private final IOaFinancePaymentService iOaFinancePaymentService;

    /**
     * 查询付款流程列表
     */
//    @SaCheckPermission("oa:financePayment:list")
    @GetMapping("/list")
    public TableDataInfo<OaFinancePaymentVo> list(OaFinancePaymentBo bo, PageQuery pageQuery) {
        return iOaFinancePaymentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出付款流程列表
     */
//    @SaCheckPermission("oa:financePayment:export")
    @Log(title = "付款流程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaFinancePaymentBo bo, HttpServletResponse response) {
        List<OaFinancePaymentVo> list = iOaFinancePaymentService.queryList(bo);
        ExcelUtil.exportExcel(list, "付款流程", OaFinancePaymentVo.class, response);
    }

    /**
     * 获取付款流程详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("oa:financePayment:query")
    @GetMapping("/{id}")
    public R<OaFinancePaymentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iOaFinancePaymentService.queryById(id));
    }

    /**
     * 新增付款流程
     */
//    @SaCheckPermission("oa:financePayment:add")
    @Log(title = "付款流程", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<OaFinancePaymentBo> add(@Validated(AddGroup.class) @RequestBody OaFinancePaymentBo bo) {
        if (StringUtils.isNotBlank(bo.getProjectName())) {
            OaFinancePaymentVo paymentVo = iOaFinancePaymentService.queryByProjectName(bo.getProjectName());
            if (ObjectUtil.isNotEmpty(paymentVo)) {
                return R.fail(bo.getProjectName() + "已经存在,不能重复申请.");
            }
        }
        return R.ok(iOaFinancePaymentService.insertByBo(bo));
    }

    /**
     * 修改付款流程
     */
//    @SaCheckPermission("oa:financePayment:edit")
    @Log(title = "付款流程", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<OaFinancePayment> edit(@Validated(EditGroup.class) @RequestBody OaFinancePaymentBo bo) {
        return R.ok(iOaFinancePaymentService.updateByBo(bo));
    }

    /**
     * 删除付款流程
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("oa:financePayment:remove")
    @Log(title = "付款流程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iOaFinancePaymentService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 获取没有采购流程的项目信息
     *
     */
    @GetMapping("/getProjectList")
    public R<List<OaProjectVo>> getProjectList() {
        return R.ok(iOaFinancePaymentService.getProjectList());
    }


    @RequestMapping("/queryByProjectName")
    public R<OaFinancePaymentVo> queryByProjectName(@RequestBody Map<String,Object> map) {
        return R.ok(iOaFinancePaymentService.queryByProjectName(map.get("projectName").toString()));
    }
}
